package bank;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;

import persitance.PersitanceManager;


public abstract class AbstractModel implements Serializable {

    private static final long serialVersionUID = 100L;

    private transient PropertyChangeSupport changeSupport;


    protected AbstractModel() {
        changeSupport = new PropertyChangeSupport(this);
        PersitanceManager.getInstance().registerModel(this);
    }

    private synchronized void readObject(ObjectInputStream aStream) throws IOException, ClassNotFoundException {
        changeSupport = new PropertyChangeSupport(this);
        PersitanceManager.getInstance().registerModel(this);
    }

    /**
     * @see PropertyChangeSupport#firePropertyChange(String, boolean, boolean)
     */
    protected void firePropertyChange(String aPropertyName, boolean aOldValue, boolean aNewValue) {
        changeSupport.firePropertyChange(aPropertyName, aOldValue, aNewValue);
    }

    /**
     * @see PropertyChangeSupport#firePropertyChange(String, int, int)
     */
    protected void firePropertyChange(String aPropertyName, int aOldValue, int aNewValue) {
        changeSupport.firePropertyChange(aPropertyName, aOldValue, aNewValue);
    }

    /**
     * @see PropertyChangeSupport#firePropertyChange(String, Object, Object)
     */
    protected void firePropertyChange(String aPropertyName, Object aOldValue, Object aNewValue) {
        changeSupport.firePropertyChange(aPropertyName, aOldValue, aNewValue);
    }

    /**
     * @see PropertyChangeSupport#addPropertyChangeListener(PropertyChangeListener)
     */
    public void addPropertyChangeListener(PropertyChangeListener aListener) {
        changeSupport.addPropertyChangeListener(aListener);
    }

    /**
     * @see PropertyChangeSupport#addPropertyChangeListener(String, PropertyChangeListener)
     */
    public void addPropertyChangeListener(String aPropertyName, PropertyChangeListener aListener) {
        changeSupport.addPropertyChangeListener(aPropertyName, aListener);
    }
}
